package cn.lx.auth.secutity.sso;

import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import cn.lx.auth.secutity.entity.PortalUserDetails;
import cn.lx.auth.secutity.utils.ApiVerifyUtil;
import cn.lx.auth.secutity.utils.AuthPropertiesUtil;
import cn.lx.auth.secutity.utils.SessionToUserUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName SSOUserContextFilter
 * @Description: //todo
 * @Author: 落雪
 * @CreateDate: 2025/7/14 23:25
 * @UpdateUser: 落雪
 * @UpdateDate: 2025/7/14 23:25
 * @UpdateRemark:
 * @Version: 1.0
 */
public class SSOUserContextFilter implements Filter {
    public SSOUserContextFilter() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String securityType = AuthPropertiesUtil.securityType;
        if (securityType != null && !securityType.equals("hsa-sso-session")) {
            SessionToUserUtil sessToUser = new SessionToUserUtil();
            sessToUser.toMockUser();
            chain.doFilter(request, response);
        } else {
            HttpServletRequest httprequest = (HttpServletRequest)request;
            HttpServletResponse httpresponse = (HttpServletResponse)response;
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null) {
                if (httprequest.getSession().getAttribute("CurrentUser") != null) {
                    PortalUserDetails userDetalis = (PortalUserDetails)httprequest.getSession().getAttribute("CurrentUser");
                    SessionToUserUtil sessToUser = new SessionToUserUtil();
                    sessToUser.toCurrentUser(userDetalis);
                    chain.doFilter(request, response);
                } else {
                    ApiVerifyUtil apiVerifyUtil = new ApiVerifyUtil();
                    if (apiVerifyUtil.whiteListVerify(httprequest.getServletPath().toString())) {
                        chain.doFilter(request, response);
                    } else {
                        httpresponse.setCharacterEncoding("UTF-8");
                        httpresponse.setContentType("application/json; charset=utf-8");
                        PrintWriter out = httpresponse.getWriter();
                        JSONObject res = new JSONObject();
                        res.put("code", -4);
                        res.put("type", WrapperResponse.ResponseType.TYPE_ERROR);
                        res.put("message", "调用非白名单，且无法获取用户信息!");
                        res.put("data", "");
                        out.append(res.toString());
                        out.flush();
                        out.close();
                    }
                }
            }
        }

    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void destroy() {
    }
}

